const std = @import("std");
const zig_live_server = @import("zig_live_server");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    // 获取命令行参数
    const args = try std.process.argsAlloc(allocator);
    defer std.process.argsFree(allocator, args);

    // 默认端口和根目录
    var port: u16 = 5000;
    var root_path: []const u8 = ".";

    // 解析命令行参数
    if (args.len > 1) {
        port = std.fmt.parseInt(u16, args[1], 10) catch 5000;
    }

    if (args.len > 2) {
        root_path = args[2];
    }

    // 创建并启动服务器
    var server = zig_live_server.Server.init(allocator, port, root_path);
    try server.start();
}

test "simple test" {
    const gpa = std.testing.allocator;
    var list: std.ArrayList(i32) = .empty;
    defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
    try list.append(gpa, 42);
    try std.testing.expectEqual(@as(i32, 42), list.pop());
}

test "fuzz example" {
    const Context = struct {
        fn testOne(context: @This(), input: []const u8) anyerror!void {
            _ = context;
            // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
            try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
        }
    };
    try std.testing.fuzz(Context{}, Context.testOne, .{});
}
